package org.dromara.system.listener;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.bo.WbMinuteListBo;

import org.dromara.system.domain.vo.WbMinuteListVo;
import org.dromara.system.service.IWbCompanyInfoService;
import org.dromara.system.service.IWbMinuteListService;
import java.util.List;

/**
 * 系统用户自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class WbMinuteListListener extends AnalysisEventListener<WbMinuteListVo> implements ExcelListener<WbMinuteListVo> {

    private final IWbMinuteListService wbMinuteListService;
    private final IWbCompanyInfoService wbCompanyInfoService;


    private Long wbMinuteBidBagId;
    private int successNum = 0;
    private int failureNum = 0;
    private int updateNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public WbMinuteListListener(Long wbMinuteBidBagId) {
        this.wbMinuteListService = SpringUtils.getBean(IWbMinuteListService.class);
        this.wbCompanyInfoService = SpringUtils.getBean(IWbCompanyInfoService.class);
        this.wbMinuteBidBagId = wbMinuteBidBagId;
    }

    @Override
    public void invoke(WbMinuteListVo wbMinuteListVo, AnalysisContext context) {
        try {
            if(wbMinuteBidBagId != null){
                WbMinuteListBo wbMinuteListBo = BeanUtil.toBean(wbMinuteListVo, WbMinuteListBo.class);
                wbMinuteListBo.setWbMinuteBidBagId(wbMinuteBidBagId);
                WbMinuteListBo wbMinuteListBo1 = new WbMinuteListBo();
                wbMinuteListBo1.setWbMinuteBidBagId(wbMinuteBidBagId);
                if (StringUtils.isNotEmpty(wbMinuteListBo.getSubStandardId())){
                    wbMinuteListBo1.setSubStandardId(wbMinuteListBo.getSubStandardId());
                }
                if (StringUtils.isNotEmpty(wbMinuteListBo.getSubStandardName())){
                    wbMinuteListBo1.setSubStandardName(wbMinuteListBo.getSubStandardName());
                }
                if (StringUtils.isNotEmpty(wbMinuteListBo.getBagNo())){
                    wbMinuteListBo1.setBagNo(wbMinuteListBo.getBagNo());
                }
                List<WbMinuteListVo> wbMinuteListVos = wbMinuteListService.queryList1(wbMinuteListBo1);
                if (wbMinuteListVos.size() <= 0) {
                    wbMinuteListService.insertByBo(wbMinuteListBo);
                    successNum++;
                }else {
                    Long id = wbMinuteListVos.get(0).getId();
                    wbMinuteListBo.setId(id);
                    wbMinuteListService.updateByBo(wbMinuteListBo);
                    updateNum++;
                }
                wbCompanyInfoService.handleName(wbMinuteListBo.getCompanyName());
                successMsg.append("<br/>").append(successNum).append("、分标编号 ").append(wbMinuteListVo.getCompanyName()).append(" 导入成功");
            }else{
                failureNum++;
                String msg = "<br/>" + failureNum + "、分标编号 " + wbMinuteListVo.getSubStandardName() + " 导入失败：";
                failureMsg.append(msg).append("无项目ID或文件缺失分标编号");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、分标编号 " + wbMinuteListVo.getSubStandardName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<WbMinuteListVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！新增共 " + successNum + " 条，修改共，"+ updateNum +"条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<WbMinuteListVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
